'use strict';

const Controller = require('egg').Controller;

class IndexController extends Controller {
  async base64() {
    this.ctx.validate({
      type: {
        type: 'enum',
        values: [ 'encode', 'decode', 'hexEncode', 'hexDecode' ],
      },
      code: {
        type: 'string',
        min: 1,
      },
    });
    const data = await this.ctx.service.coding.base64();
    this.ctx.sendSuccess(data);
  }
  async imgToBase64() {
    this.ctx.validate({
      imageUrl: {
        type: 'string',
        min: 1,
      },
    });
    const data = await this.ctx.service.coding.imgToBase64();
    this.ctx.sendSuccess(data);
  }
  async base64ToImg() {
    this.ctx.validate({
      imageData: {
        type: 'string',
        min: 1,
      },
    });
    const data = await this.ctx.service.coding.base64ToImg();
    this.ctx.sendSuccess(data);
  }
  async md5() {
    this.ctx.validate({
      type: {
        type: 'enum',
        values: [ 'md5', 'sha1', 'sha256', 'sha512' ],
      },
      input: {
        type: 'string',
        min: 1,
      },
    });
    const data = await this.ctx.service.coding.md5();
    this.ctx.sendSuccess(data);
  }
  async hmac() {
    this.ctx.validate({
      type: {
        type: 'enum',
        values: [ 'md5', 'sha1', 'sha256', 'sha512' ],
      },
      input: {
        type: 'string',
        min: 1,
      },
      key: {
        type: 'string',
        min: 1,
      },
    });
    const data = await this.ctx.service.coding.hmac();
    this.ctx.sendSuccess(data);
  }
  async aes() {
    this.ctx.validate({
      type: {
        type: 'enum',
        values: [ 'encode', 'decode' ],
      },
      input: {
        type: 'string',
        min: 1,
      },
      key: {
        type: 'string',
        length: 6,
      },
    });
    const data = await this.ctx.service.coding.aes();
    this.ctx.sendSuccess(data);
  }
}

module.exports = IndexController;
